var api = require("../../api.js");
var r, tips, app = getApp(); 
Page({
  /** 页面的初始数据 */
  data: {
    name: '',
    phone: '',
    introduction: '',
    companyName: '',
    expertise: '',
    offer: '',
    businessScale: '',
    s_e_index: -1,
    q_i_index: -1,
    b_s_index: -1,
    upload_picture_list: [],
    //普通选择器：（普通数组）
    skill_expertise: ['策略（含顶层设计、商业模式等）', '营销（传统型）', '新营销（含电商、微商、新媒体等）', 'VI（视觉识别系统）', '工业设计', 'SI（企业终端形象识别系统）', '整合传播（含公关、媒介、会展等）','管理咨询'],
    /** 报价区间 */
    quotation_interval:['50万以内','50到100万','100到200万','200万以上'],
    /** 业务规模 */
    business_scale: ['年收入少于300万元','年收入300~800万元','年收入800万元以上'],
    /** 营业执照 */
    certificatesImg: '../../images/upload.png',
    /** 个人形象照 */
    headImg: '../../images/upload.png',
  },
  onLoad: function (options) {
    r = this;
    /** 检测是否授权 */
    getApp().is_empower();
  },
  onShow: function () {
    var that = this;
    r.toast = that.selectComponent("#toast");
    /** 顶部提示的插件 */
    tips = that.selectComponent("#mytips");
  },
  change_s_picker:function(e){
    this.setData({
      s_e_index: e.detail.value,
      expertise: e.detail.value
    })
  },
  change_q_picker: function (e) {
    this.setData({
      q_i_index: e.detail.value,
      offer: e.detail.value
    })
  },
  change_b_picker: function (e) {
    this.setData({
      b_s_index: e.detail.value,
      businessScale: e.detail.value
    })
  },
  /** 选择图片 */
  choose_img: function (e) {
    var _that = this;
    let type = e.currentTarget.dataset.imgtype;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        console.log(res)
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: api.img.upload_img, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {},
          success(resp) {
            const data = JSON.parse(resp.data).data
            _that.setData({
              [type]: data.url
            })
            console.log(data.url)
          }
        })
      }
    })
  },
  /** 提交申请 */
  submit: function () {
    /** 验证输入 */
    if (this.data.name == '' || this.data.name == null) {
      tips.showTopTip('姓名不能为空'); return;
    };
    if (this.data.phone == '' || this.data.phone == null) {
      tips.showTopTip('手机号不能为空'); return;
    };
    if (this.data.introduction == '' || this.data.introduction == null) {
      tips.showTopTip('个人简介不能为空'); return;
    };
    if (this.data.companyName == '' || this.data.companyName == null) {
      tips.showTopTip('单位名称不能为空'); return;
    };
    if (this.data.headImg == '../../images/upload.png') {
      tips.showTopTip('请上传个人形象照'); return;
    };
    if (this.data.certificatesImg == '../../images/upload.png') {
      tips.showTopTip('请上传营业执照'); return;
    };
    if (this.data.expertise == '' || this.data.expertise == null) {
      tips.showTopTip('技能专长称不能为空'); return;
    };
    if (this.data.companyName == '' || this.data.companyName == null) {
      tips.showTopTip('报价区间不能为空'); return;
    };
    if (this.data.companyName == '' || this.data.companyName == null) {
      tips.showTopTip('业务规模不能为空'); return;
    };

    let sendData = {
      openId: getApp().globalData.open_id,
      name: this.data.name,
      phone: this.data.phone,
      introduction: this.data.introduction,
      companyName: this.data.companyName,
      expertise: this.data.expertise,
      offer: this.data.offer,
      businessScale: this.data.businessScale,
      certificatesImg: this.data.certificatesImg,
      headImg: this.data.headImg,
    }

    wx.showModal({
      title: '提示',
      content: '请保证所填写的信息真实有效,否则将审核不通过',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: api.mine.think_tank_aut,
            data: sendData,
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            method: 'POST',
            success(res) {
              /** 申请认证成功 */
              if (res.data.code == 200) {
                wx.showToast({
                  title: '申请成功',
                  icon: 'none',
                  duration: 2000
                })
              }
              /** 已申请认证 */
              if (res.data.code == 1005) {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          });
        } else {
          console.log('用户点击取消')
        }
      }
    })
  },
  /** 值绑定事件 */
  bindKeyInput: function (e) {
    let item = e.currentTarget.dataset.model;
    this.setData({
      [item]: e.detail.value
    });
  },
  /** 用户分享事件 */
  onShareAppMessage: function (res) {
    return {
      title: '智库专家认证',
      path: `pages/think_tank_aut/parner`,
      success: function (res) { },
      fail: function (res) { }
    }
  },
});
